package cn.m3a1.control.student;

import cn.m3a1.bean.AbsentNote;
import cn.m3a1.bean.Curse;
import cn.m3a1.bean.Tclass;
import cn.m3a1.bean.User;
import cn.m3a1.service.student.StuFormService;
import cn.m3a1.util.GlobalUtils;
import com.sun.tools.javac.jvm.Items;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.List;
import java.util.UUID;

/**
 * @author wyj
 * @date 2019/7/12
 * @time 下午7:15
 */

@Controller
@RequestMapping("/student")
public class StuLeaveController {
    @Autowired
    StuFormService stuFormService;

    /**
     * 携带参数跳转到请假表单页面
     *
     * @param modelMap
     * @param request
     * @return
     */
    @RequestMapping("stuReqForm")
    public String stuReqForm(ModelMap modelMap, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User loginUser = (User) session.getAttribute("loginUser");
        modelMap.put("loginUser", loginUser);
        List<Curse> curses = stuFormService.getCurses(loginUser.getId());
        modelMap.put("curses", curses);
        return "student/stuleave";
    }


    @RequestMapping("/upload")
    public String upload(MultipartFile picfile, AbsentNote absentNote, ModelMap modelMap, HttpServletRequest request) throws Exception {
        HttpSession session = request.getSession();
        User loginUser = (User) session.getAttribute("loginUser");
        //1.获取图片名称
        String filename = picfile.getOriginalFilename();
        //2.使用随机生成的字符串加图片后缀组成新的图片名，防止重复
        String newfilename = UUID.randomUUID().toString() + filename.substring(filename.lastIndexOf("."));
        //3.将图片存入硬盘
        picfile.transferTo(new File("/Users/wyj/Downloads/upload/" + newfilename));
        //4.将相应的信息存入数据库
        absentNote.setTimestamp(System.currentTimeMillis());
        absentNote.setExtfile(newfilename);
        absentNote.setStudentid(loginUser.getId());

        List<Tclass> tclass = stuFormService.getTclass(loginUser.getId());
        System.out.println(absentNote.toString());
        if (tclass.isEmpty()) {
            //该学生没有班级，跳转异常页面
        }
        if (GlobalUtils.isEmpty(absentNote.getReason())) {
            //没有理由，跳转错误页面
        }

        absentNote.setClassid(tclass.get(0).getId());
        absentNote.setManagerid(tclass.get(0).getManagerid());
        if (absentNote.getType() == 2) {
            System.out.println("执行了。。。。");
            List<Curse> curses = stuFormService.getCurse(absentNote.getCourseid());
            System.out.println("教师的id为："+curses.get(0).getTeacherid());
            absentNote.setTeacherid(curses.get(0).getTeacherid());
        }



        Boolean aBoolean = stuFormService.saveAbsentNote(absentNote);

        return " 异常";
    }
@RequestMapping("/myLeave")
    public  String  myLeave(ModelMap modelMap,HttpServletRequest request){
        HttpSession session = request.getSession();
        User loginUser = (User) session.getAttribute("loginUser");
        List<AbsentNote> myleave = stuFormService.getMyleave(loginUser.getId());
        modelMap.put("loginUser",loginUser);
        modelMap.put("members",myleave);
        return "student/myleave";
    }
}
